package com.park.service.impl.article;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.park.client.model.article.ArticleCommentModel;
import com.park.client.model.pub.SensitiveWordModel;
import com.park.client.model.user.UserMsgModel;
import com.park.client.service.article.ArticleCommentService;
import com.park.client.service.pub.SensitiveWordService;
import com.park.client.service.user.UserMsgService;
import com.park.common.constant.Constant;
import com.park.common.empty.EmptyUtils;
import com.park.common.enums.ArticleStatusEnum;
import com.park.dao.article.ArticleCommentMapper;
import com.park.service.impl.base.BaseServiceImpl;

@Service("articleCommentService")
public class ArticleCommentServiceImpl extends BaseServiceImpl<ArticleCommentModel> implements ArticleCommentService {
	private static final Logger logger = LoggerFactory.getLogger(ArticleCommentServiceImpl.class);

	@Autowired
	private  ArticleCommentMapper  articleCommentMapper;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	@Autowired
	private UserMsgService userMsgService;

	@Override
	public boolean deleteByPrimaryKey(Long id) {
		return articleCommentMapper.deleteByPrimaryKey(id)==1;
	}
	@Override
	public boolean insertSelective(ArticleCommentModel record) {
		return articleCommentMapper.insertSelective(record)==1;
	}
	@Override
	public ArticleCommentModel selectByPrimaryKey(Long id) {
		return articleCommentMapper.selectByPrimaryKey(id);
	}
	@Override
	public boolean updateByPrimaryKeySelective(ArticleCommentModel record) {
		return articleCommentMapper.updateByPrimaryKeySelective(record)==1;
	}
	@Override
	public boolean updateByPrimaryKey(ArticleCommentModel record) {
		return articleCommentMapper.updateByPrimaryKey(record)==1;
	}
	@Override
	public boolean insert(ArticleCommentModel record) {
		return articleCommentMapper.insert(record)==1;
	}
	@Override
	public List<ArticleCommentModel> selectAll() {
		return articleCommentMapper.selectAll();
	}
	@Override
	public ArticleCommentModel selectIsExist(ArticleCommentModel record) {
		return articleCommentMapper.selectIsExist(record);
	}
	@Override
	public List<ArticleCommentModel> selectPagination(ArticleCommentModel t) {
		return articleCommentMapper.selectPagination(t);
	}
	@Override
	public Integer selectTotal(ArticleCommentModel t) {
		return articleCommentMapper.selectTotal(t);
	}
	@Override
	public List<ArticleCommentModel> selectByStatus(Integer status) {
		return articleCommentMapper.selectByStatus(status);
	}
	
	@Override
	public void ArticleCommentAutoAudit() {
		//查询待审核的文章
		List<ArticleCommentModel> articleCommentList  = this.selectByStatus(ArticleStatusEnum.STATUS_1.code);
		if(EmptyUtils.isEmpty(articleCommentList)){
			return;
		}
		logger.info("======================{}",articleCommentList);
		//检查有没有包含敏感字
		List<SensitiveWordModel> sensitiveWordList = sensitiveWordService.selectAll();
		if(EmptyUtils.isEmpty(sensitiveWordList)){//没有设置敏感字，则直接更新为显示
			this.batchUpdate(articleCommentList);
		}else{
			List<ArticleCommentModel> newList = new ArrayList<>();
			for (ArticleCommentModel comment : articleCommentList) {
				String resultMsg = checkWord(comment, sensitiveWordList);
				//包含敏感字
				if(EmptyUtils.isNotEmpty(resultMsg)){
					//发送消息提醒
					UserMsgModel msg = new UserMsgModel();
					msg.setMsgTitle(Constant.SYSTEM_MSG_TITLE);
					msg.setMsgContent(MessageFormat.format(Constant.SYSTEM_MSG_CONTENT, resultMsg));
					msg.setCreater(comment.getCreater());
					userMsgService.saveMsg(msg,comment.getCreater());
					comment.setAuditCount(comment.getAuditCount()+1);
					this.updateByPrimaryKey(comment);
					continue;
				}else{
					//审核通过，修改状态
					newList.add(comment);
				}
			}
			if(EmptyUtils.isNotEmpty(newList)){
				this.batchUpdate(newList);
			}
		}
	}
	
	private String checkWord(ArticleCommentModel comment,List<SensitiveWordModel> sensitiveWordList){
		StringBuilder builder = new StringBuilder();
		for (SensitiveWordModel sensitiveWord : sensitiveWordList) {
			boolean isContain = comment.getCommentContent().toUpperCase().contains(sensitiveWord.getWord().toUpperCase());
			if(isContain){
				builder.append(sensitiveWord.getWord());
			}
		}
		return builder.toString();
	}
	
	@Override
	public int batchUpdate(List<ArticleCommentModel> records) {
		return articleCommentMapper.batchUpdate(records);
	}

}
